package com.example.designpatterns.behavioral.chain;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 客户端代码（具体使用）
 *
 * @author scott
 */
public class Client {

    private static final BufferedReader READER = new BufferedReader(new InputStreamReader(System.in));
    private static Server server;

    /**
     * 初始化
     */
    private static void init() {
        server = new Server();
        server.register("admin@example.com", "admin_pass");
        server.register("user@example.com", "user_pass");

        // 构建处理者链
        Middleware middleware = Middleware.link(
                new ThrottlingMiddleware(2),
                new UserExistsMiddleware(server),
                new RoleCheckMiddleware()
        );

        // 设置处理者链
        server.setMiddleware(middleware);
    }

    /**
     * 模拟登录
     */
    public static void main(String[] args) throws IOException {
        init();

        boolean success;
        do {
            System.out.print("请输入邮箱：");
            String email = READER.readLine();
            System.out.print("请输入密码：");
            String password = READER.readLine();
            success = server.logIn(email, password);
        } while (!success);
    }

}
